package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.TokenPermission;
import com.ktgj.customer.domain.Employee;
import com.ktgj.customer.domain.EmployeeVipresource;
import com.ktgj.customer.domain.dto.CustomerInfoDTO;
import com.ktgj.customer.domain.vo.CustomerCardGroupVO;
import com.ktgj.customer.domain.vo.CustomerResource;
import com.ktgj.customer.domain.vo.SaveDistributeVo;
import com.ktgj.customer.service.IEmployeeService;
import com.ktgj.customer.service.ILoginService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 员工Controller
 * 
 * @author ruoyi
 * @date 2021-07-27
 */
@ControllerAdvice
@RestController
@RequestMapping("/employee")
public class EmployeeController extends BaseController
{
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询员工列表
     */
//    @PreAuthorize(hasPermi = "system:employee:list")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody Employee employee)
    {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
//    @PreAuthorize(hasPermi = "system:employee:export")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Employee employee) throws IOException
    {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, list, "员工数据");
    }

    /**
     * 获取员工详细信息
     */
//    @PreAuthorize(hasPermi = "system:employee:query")
    @GetMapping(value = "/{employeeId}")
    public AjaxResult getInfo(@PathVariable("employeeId") Long employeeId)
    {
        return AjaxResult.success(employeeService.selectEmployeeById(employeeId));
    }

    /**
     * 根据员工id获取员工信息
     * @param map
     * @return
     */
    @PostMapping("/getInfoById")
    public AjaxResult getInfoById(@RequestBody Map<String, Long> map)
    {
        Long employeeId = map.get("employeeId");
        return AjaxResult.success(employeeService.selectEmployeeById(employeeId));
    }

    /**
     * 新增员工
     */
//    @PreAuthorize(hasPermi = "system:employee:add")
    @Log(title = "员工", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody List<Employee> employeeList) {
        int i;
        try {
            i = employeeService.insertEmployee(employeeList);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("新增成功");
        }
        else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改员工
     */
//    @PreAuthorize(hasPermi = "system:employee:edit")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody List<Employee> employeeList) {
        int i;
        try {
            i = employeeService.updateEmployee(employeeList);
        } catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }

        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除员工
     */
//    @PreAuthorize(hasPermi = "system:employee:remove")
    @Log(title = "员工", businessType = BusinessType.DELETE)
	@DeleteMapping("/{employeeIds}")
    public AjaxResult remove(@PathVariable Long[] employeeIds)
    {
        return toAjax(employeeService.deleteEmployeeByIds(employeeIds));
    }

    @PostMapping("/resetPassowrd")
    public AjaxResult resetPassowrd(@RequestBody Map<String,Long> map)
    {
        Long empId = map.get("employeeId");
        if (employeeService.resetEmployeePassword(empId))
        {
           return AjaxResult.success("重置密码成功！");
        }else {
           return AjaxResult.error("重置密码失败！");
        }
    }

    //设置员工离职
    @PostMapping("/quit")
    public AjaxResult quit(@RequestBody Map<String, Long> map) {
        return toAjax(employeeService.quit(map.get("employeeId")));
    }

    /**
     * 新增vip经理
     * */
//    @PostMapping("/addvipManager")
//    public AjaxResult addvipManager(@RequestBody Employee employee) {
//        int i = employeeService.addvipManager(employee);
//        if (i > 0){
//            return AjaxResult.success("新增成功");
//        }
//        else {
//            return AjaxResult.error("新增失败");
//        }
//    }

    /**
     * 转移
     * */
//    @PostMapping("/updateEmployeeVipresource")
//    public AjaxResult updateEmployeeVipresource(HttpServletRequest request, @RequestBody EmployeeVipresource employeeVipresource){
//        String token = request.getHeader("token");
//        Employee employee = loginService.getLoginEmployeeByToken(token);
//        employeeVipresource.setId(employee.getDeptId());
//        int i = employeeService.updateEmployeeVipresource(employeeVipresource);
//        if (i > 0){
//            return AjaxResult.success("转移成功");
//        }
//        else {
//            return AjaxResult.error("转移失败");
//        }
//    }

    /**
     * 分配
     * */
//    @PostMapping("/distribute")
//    public AjaxResult distribute(HttpServletRequest request) {
//        String token = request.getHeader("token");
//        Employee employee = loginService.getLoginEmployeeByToken(token);
//        int distribute = employeeService.distribute(employee.getEmployeeId());
//        if (distribute > 0){
//            return AjaxResult.success("分配成功");
//        }
//        else {
//            return AjaxResult.success("分配失败");
//        }
//    }
//    /**
//     * 查询VIP经理手下的会员明细
//     * */
//    @PostMapping("/selectEmployeeVipresourceList")
//    public TableDataInfo selectEmployeeVipresourceList(@RequestBody Map<String,Object> map) {
//        startPage();
//        Long employeeId=new Long(map.get("employeeId").toString());
//        List<EmployeeVipresource>list= employeeService.selectEmployeeVipresourceList(employeeId);
//        return  getDataTable(list);
//    }
    /**
     * 查询在职的VIP经理
     * */
    @PostMapping("/selectEmployeeVip")
    public AjaxResult selectEmployeeVip(@RequestBody Map<String, Object> map) {
        Long deptId;
        if (map.get("deptId") != null && !map.get("deptId").equals("")){
            deptId = Long.valueOf(map.get("deptId").toString());
        }
        else {
            deptId = 0l;
        }
         List<Employee> list = employeeService.selectEmployeeVip(deptId);
         if(list.size() > 0) {
             return AjaxResult.success(list);
         }
         else {
             return AjaxResult.error("相关数据为空");
         }
    }

    /**
     * 分配VIP经理会员资源
     * @param lstCustomerCardGroup
     * @return
     */
//    @PostMapping("/distributeCustomerInfo")
//    public AjaxResult distributeCustomerInfo(@RequestBody List<CustomerCardGroupVO> lstCustomerCardGroup)
//    {
//        Long employeeId = lstCustomerCardGroup.get(0).getEmployeeId();
//        return AjaxResult.success(employeeService.distributeCustomerInfo(employeeId,lstCustomerCardGroup));
//    }
    /**
     * 查询员工列表
     */
//    @PreAuthorize(hasPermi = "system:employee:list")
    @PostMapping ("/getEmployeeByPosition")
    public AjaxResult list(@RequestBody Map<String,Object> map)
    {
        Employee employee=new Employee();
        employee.setPositionId(new Long(map.get("positionId").toString()));
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return AjaxResult.success(list);
    }

    /**
     * 员工姓名获取id
     * */
    @PostMapping("/employeeIdList")
    public List<Long> employeeIdList(@RequestBody Map<String, Object> map){
        String name = map.get("name").toString();
        List<Long> employeeId = employeeService.employeeIdList(name);
        return employeeId;
    }

    /**
     * 员工id获取姓名
     * */
    @PostMapping("/employeeNameList")
    public Employee employeeNameList(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        Employee employee = employeeService.employeeNameList(id);
        return employee;
    }

    /**
     * 获取指定部门的所有员工id
     * */
    @PostMapping("/employeeVipIdList")
    public List<Long> employeeVipIdList(@RequestBody Map<String, Object> map) {
        Long deptId = Long.valueOf(map.get("deptId").toString());
        List<Long> longList = employeeService.employeeVipIdList(deptId);
        return longList;
    }

    /**
     * 根据ID列表获取员工列表
     * @param lstIds
     * @return
     */
    @PostMapping("/getEmployeeListByIds")
    public List<Employee> getEmployeeListByIds(@RequestBody List<Long> lstIds)
    {
        return employeeService.getEmployeeListByIds(lstIds);
    }

    /**
     * 保存vip经理分配信息
     * */
    @PostMapping("/saveDistribute")
    public AjaxResult saveDistribute(HttpServletRequest request, @RequestBody SaveDistributeVo saveDistributeVo)
    {
        boolean result;
        try
        {
            String token = request.getHeader("token");
            Employee employee = loginService.getLoginEmployeeByToken(token);
            if (employee == null)
            {
                return AjaxResult.error("登录已过期");
            }
            result = employeeService.saveDistribute(employee, saveDistributeVo);
        } catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
        if (result)
        {
            return AjaxResult.success("保存成功");
        } else
        {
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 获取待分配的会员列表
     * @param customerResource
     * @return
     */
    @PostMapping("/customerResourceList")
    public AjaxResult getCustomerResourceList(@RequestBody CustomerResource customerResource)
    {
        return AjaxResult.success(employeeService.getCustomerResource(customerResource));
    }

    /**
     * 分配VIP资源
     */
    @PostMapping("/distributeVipresouce")
    public AjaxResult distributeCustomer(HttpServletRequest request, @RequestBody List<CustomerInfoDTO> customerInfo)
    {
        boolean result;
        try
        {
            String token = request.getHeader("token");
            Employee employee = loginService.getLoginEmployeeByToken(token);
            if (employee == null)
            {
                return AjaxResult.error("登录已过期");
            }
            result = employeeService.distributeCustomer(employee, customerInfo);
        } catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
        if (result)
        {
            return AjaxResult.success("保存成功");
        } else
        {
            return AjaxResult.error("保存失败");
        }
    }

    /**
     * 转移VIP经理资源
     * @param map
     * @return
     */
    @PostMapping("/changeVipresource")
    public AjaxResult changeVipresource(@RequestBody Map<String, Long> map)
    {
        Long sourceId = map.get("sourceId");
        Long targetId = map.get("targetId");
        Long customerId = map.get("customerId");
        boolean result = employeeService.changeVipresource(sourceId, targetId, customerId);
        if (result)
        {
            return AjaxResult.success("转移VIP资源成功！");
        } else
        {
            return AjaxResult.error("转移VIP资源失败！");
        }
    }

    /**
     * 获取当前在线员工列表
     * @param
     * @return
     */
    @PostMapping("/getOnlineEmployeeList")
    public List<Employee> getOnlineEmployeeList()
    {
        return employeeService.getOnlineEmployeeList();
    }
}

